package com.wbao.dianniu.update;

import com.wbao.dianniu.data.KnowledgeData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yejianjun on 2017/9/21.
 */

public class KnowledgeHelper {


    private static KnowledgeHelper instance;
    private List<IKnowledgeChangedListenr> mListener = null;

    public static KnowledgeHelper getInstance() {
        if (null == instance) {
            instance = new KnowledgeHelper();
        }
        return instance;
    }

    private void KnowledgeHelper() {
        mListener = new ArrayList<>();
    }

    public interface IKnowledgeChangedListenr {
        public void addOneKnowledge(KnowledgeData data);

        public void removeOneKnowledge(int knowledgeId);

        public void goodNotify(int knowledgeId, int isGood, int goodCount);

        public void replyNotify(int knowledgeId, int answerCounts);

        public void visitorNotify(int knowledgeId, int visitorCounts);

        public void modifyOneData(int knowledgeId, KnowledgeData data);
    }

    public void addListener(IKnowledgeChangedListenr listenr) {
        if (null == mListener) {
            mListener = new ArrayList<>();
        }
        mListener.add(listenr);
    }

    public void removeListener(IKnowledgeChangedListenr listener) {
        if (null != mListener) {
            mListener.remove(listener);
        }
    }

    public void addNotify(KnowledgeData data) {
        if (null != mListener) {
            for (int i = 0; i < mListener.size(); i++) {
                mListener.get(i).addOneKnowledge(data);
            }
        }
    }

    public void deleteNotify(int knowledgeId) {
        if (null != mListener) {
            for (int i = 0; i < mListener.size(); i++) {
                mListener.get(i).removeOneKnowledge(knowledgeId);
            }
        }
    }

    public void goodNotify(int knowledgeId, int isGood, int goodCount) {
        if (null != mListener) {
            for (int i = 0; i < mListener.size(); i++) {
                mListener.get(i).goodNotify(knowledgeId, isGood, goodCount);
            }
        }
    }

    public void visitorNotify(int knowledgeId, int visitorCounts) {
        if (null != mListener) {
            for (int i = 0; i < mListener.size(); i++) {
                mListener.get(i).visitorNotify(knowledgeId, visitorCounts);
            }
        }
    }

    public void replyNotify(int knowledgeId, int answerCounts) {
        if (null != mListener) {
            for (int i = 0; i < mListener.size(); i++) {
                mListener.get(i).replyNotify(knowledgeId, answerCounts);
            }
        }
    }

    public void modifyOneData(int knowledgeId, KnowledgeData data){
        if (null != mListener) {
            for (int i = 0; i < mListener.size(); i++) {
                mListener.get(i).modifyOneData(knowledgeId, data);
            }
        }
    }


}
